
// Network.cpp: implementation of the CNetwork class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Network.h"
#include "Network_guid.h"
#include "ServerTable.h"
#ifdef __AGENTSERVER__
#include "TrafficLog.h"
#include "UserTable.h"
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

BOOL g_bReady = FALSE;

CNetwork g_Network;
#ifndef _GMTOOLSERVER_
extern char g_CryptKeyForUser[];
extern char g_CryptKeyForServer[];
#endif
CNetwork::CNetwork()
{
//	CoInitialize(NULL);
	m_pINet = NULL;

}

CNetwork::~CNetwork()
{
	//Release();
//	CoUninitialize();
}
void CNetwork::Release()
{

	if(m_pINet)
	{
		m_pINet->Release();

		m_pINet = NULL;
	}
}


void CNetwork::Init(DESC_NETWORK * desc)
{
	if(!g_pServerTable) return;
	
	HRESULT hr;
	hr = CoCreateInstance( CLSID_4DyuchiNET, NULL, CLSCTX_INPROC_SERVER, IID_4DyuchiNET, (void**)&m_pINet);
	if (FAILED(hr))
		return;

	if(!m_pINet->CreateNetwork(desc,0,0,0))
		return;
}

BOOL CNetwork::StartServerServer(char * szIP, int port)
{
	return m_pINet->StartServerWithServerSide(szIP,port);
}
BOOL CNetwork::StartUserServer(char * szIP, int port)
{
	return m_pINet->StartServerWithUserSide(szIP, port);
}
BOOL CNetwork::ConnectToServer(char * szIP, int port, void * pParam)
{
	return m_pINet->ConnectToServerWithServerSide(szIP,port, OnConnectServerSuccess, OnConnectServerFail, pParam);
}

void CNetwork::Send2Server(DWORD dwConnectionIndex, char * msg, DWORD size)
{
	MSGBASE* pmsg = (MSGBASE*)msg;

	m_pINet->SendToServer(dwConnectionIndex, msg, size, FLAG_SEND_NOT_ENCRYPTION);
}

void CNetwork::Send2AgentServer(char * msg, DWORD size)
{
	MSGBASE* pmsg = (MSGBASE*)msg;

	g_pServerTable->SetPositionHead();
	SERVERINFO* pInfo;
	while(pInfo = g_pServerTable->GetNextAgentServer())
	{
		m_pINet->SendToServer(pInfo->dwConnectionIndex, msg, size, FLAG_SEND_NOT_ENCRYPTION);
		break;
	}
}
void CNetwork::SendAll2AgentServer(char * msg, DWORD size)
{
	MSGBASE* pmsg = (MSGBASE*)msg;

	g_pServerTable->SetPositionHead();
	SERVERINFO* pInfo;
	while(pInfo = g_pServerTable->GetNextAgentServer())
	{
		m_pINet->SendToServer(pInfo->dwConnectionIndex, msg, size, FLAG_SEND_NOT_ENCRYPTION);
	}
}
void CNetwork::Send2User(DWORD dwConnectionIndex, char * msg, DWORD size)
{
	BOOL rt = m_pINet->SendToUser(dwConnectionIndex, msg, size, FLAG_SEND_NOT_ENCRYPTION);
}



void CNetwork::Broadcast2Server(char * msg, DWORD size)
{
	MSGBASE* pmsg = (MSGBASE*)msg;
	m_pINet->BroadcastServer(msg, size, FLAG_SEND_NOT_ENCRYPTION);
}
void CNetwork::BroadCastDisServer(char * msg, DWORD size)
{
	g_pServerTable->SetPositionHead();
	SERVERINFO* pInfo;
	while(pInfo = g_pServerTable->GetNextDistServer())
	{
		m_pINet->SendToServer(pInfo->dwConnectionIndex, msg, size, FLAG_SEND_NOT_ENCRYPTION);
	}
}
void CNetwork::Broadcast2User(MSGBASE * msg, DWORD size)
{
	m_pINet->BroadcastUser((char*)msg, size, FLAG_SEND_NOT_ENCRYPTION);
}

void CNetwork::Broadcast2MapServer(char * msg, DWORD size)
{
	MSGBASE* pmsg = (MSGBASE*)msg;
	g_pServerTable->SetPositionHead();
	SERVERINFO* pInfo;
	while(pInfo = g_pServerTable->GetNextMapServer())
	{
		m_pINet->SendToServer(pInfo->dwConnectionIndex, msg, size, FLAG_SEND_NOT_ENCRYPTION);
	}
}

void CNetwork::Broadcast2MapServerExceptOne(DWORD dwConnectionIndex, char * msg, DWORD size)
{
	MSGBASE* pmsg = (MSGBASE*)msg;

	g_pServerTable->SetPositionHead();
	SERVERINFO* pInfo;
	while(pInfo = g_pServerTable->GetNextMapServer())
	{
		if(dwConnectionIndex == pInfo->dwConnectionIndex)
			continue;

		m_pINet->SendToServer(pInfo->dwConnectionIndex, msg, size, FLAG_SEND_NOT_ENCRYPTION);
	}	
}

void CNetwork::Broadcast2AgentServer(char* msg, DWORD size)
{
	MSGBASE* pmsg = (MSGBASE*)msg;

	g_pServerTable->SetPositionHead();
	SERVERINFO* pInfo;
	while(pInfo = g_pServerTable->GetNextAgentServer())
	{
		m_pINet->SendToServer(pInfo->dwConnectionIndex, msg, size, FLAG_SEND_NOT_ENCRYPTION);

	}
}

void CNetwork::Broadcast2AgentServerExceptSelf(char* msg, DWORD size)
{
	MSGBASE* pmsg = (MSGBASE*)msg;

	g_pServerTable->SetPositionHead();
	SERVERINFO* pInfo;
	while(pInfo = g_pServerTable->GetNextAgentServer())
	{
		m_pINet->SendToServer(pInfo->dwConnectionIndex, msg, size, FLAG_SEND_NOT_ENCRYPTION);
	}
}

void CNetwork::Broadcast2AgentServerExceptOne(DWORD dwConnectionIndex, char* msg, DWORD size)
{
	MSGBASE* pmsg = (MSGBASE*)msg;

	g_pServerTable->SetPositionHead();
	SERVERINFO* pInfo;
	while(pInfo = g_pServerTable->GetNextAgentServer())
	{

		if(dwConnectionIndex == pInfo->dwConnectionIndex)
			continue;

		m_pINet->SendToServer(pInfo->dwConnectionIndex, msg, size, FLAG_SEND_NOT_ENCRYPTION);
	}	
	
}

void CNetwork::GetUserAddress(DWORD dwConnectionIndex, char* ip, WORD * port)
{
	m_pINet->GetUserAddress(dwConnectionIndex, ip, port);
}

void CNetwork::DisconnectUser(DWORD dwConnectionIndex)
{
	m_pINet->CompulsiveDisconnectUser(dwConnectionIndex);
}




